"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

from PyQt4 import QtGui, QtCore
from msnp.gui import UIText, Resources, GUIHelper, MyStyle
from msnp.service import Initialization
from msnp import gui
from msnp.persistence import XMLController, PersistenceController
from msnp.model import SettingsModel
from msnp.service.network import LeaveRequest

class StartWidget(QtGui.QWidget):
    """This class contains the start widget which enables the user to join the network.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __settings: A Settings object containing the stores settings.
        __loginWidget: A LoginWidget object containing the login widget.
        __loginSplashWidget: A LoginSplashWidget object containing the splash widget.
        __loginMsnpDBWidget: A LoginMsnpDBSettingsWidget object containing the network database settings widget.
        __loginMediaDBWidget: A LoginMediaDBSettingsWidget object containing the media database settings widget.
        __mainLayout: A QVBoxLayout object containing one of the four widgets, __loginWidget, __loginSpashWidget, __loginMsnpDBWidget or __loginMediaDBWidget.
    """
    
    def __init__(self, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__settings = XMLController.readAllSettings()
        self.__loginWidget = None
        self.__loginSplashWidget = None
        self.__mainLayout = QtGui.QVBoxLayout()
        self.setLayout(self.__mainLayout)
        GUIHelper.centerWidget(self)
        gui.GUIController.openSystemTray()
        
    def buildLoginWidget(self):
        """Removes everything from the __mainLayout, creates the __loginWidget and adds it to the __mainLayout."""
        gui.MySignal.emitLogoutEnabledSignal(False)
        self.__cleanLayout()
        self.setWindowTitle(UIText.TITLE_LOGIN)
        self.setWindowIcon(QtGui.QIcon(Resources.IMAGE_ICON))
        self.__loginWidget = LoginWidget(self.__settings, self)
        self.__mainLayout.addWidget(self.__loginWidget)
        self.setFixedSize(400, 350)
        self.repaint()
        GUIHelper.centerWidget(self)
        
    def buildLoginSplashWidget(self):
        """Removes everything from the __mainLayout, creates the __loginSplashWidget and adds it to the __mainLayout."""
        self.__cleanLayout()
        self.setWindowTitle(UIText.TITLE_AUTHENTICATION)
        self.setWindowIcon(QtGui.QIcon(Resources.IMAGE_ICON))
        self.__loginSplashWidget = LoginSplashWidget()
        self.__mainLayout.addWidget(self.__loginSplashWidget)
        self.setFixedSize(400, 350)
        self.repaint()
        GUIHelper.centerWidget(self)
        
    def buildLoginMsnpDBSettingsWidget(self, settings, username, password, isLogin, isNewNetwork):
        """Removes everything from the __mainLayout, creates the __loginMsnpDBWidget and adds it to the __mainLayout."""
        self.__cleanLayout()
        self.setWindowTitle(UIText.TITLE_MSNP_DB_SETTINGS)
        self.setWindowIcon(QtGui.QIcon(Resources.IMAGE_ICON))
        self.__loginMsnpDBWidget = LoginMsnpDBSettingsWidget(username, password, isLogin, isNewNetwork, self)
        self.__mainLayout.addWidget(self.__loginMsnpDBWidget)
        self.setFixedSize(400, 350)
        self.repaint()
        GUIHelper.centerWidget(self)
        
    def buildLoginMediaDBSettingsWidget(self, settings, username, password, isLogin, isNewNetwork):
        """Removes everything from the __mainLayout, creates the __loginMediaDBWidget and adds it to the __mainLayout."""
        self.__cleanLayout()
        self.setWindowTitle(UIText.TITLE_MEDIA_DB_SETTINGS)
        self.setWindowIcon(QtGui.QIcon(Resources.IMAGE_ICON))
        self.__loginMediaDBWidget = LoginMediaDBSettingsWidget(username, password, isLogin, isNewNetwork, self)
        self.__mainLayout.addWidget(self.__loginMediaDBWidget)
        self.setFixedSize(400, 350)
        self.repaint()
        GUIHelper.centerWidget(self)
        
    # remove widgets from layout when building a new one
    def __cleanLayout(self):
        """Removes everything from the __mainLayout."""
        import sip
        if self.layout() != None:
            oldLayout = self.layout()
            for i in reversed(range(oldLayout.count())):
                oldLayout.itemAt(i).widget().setParent(None)
            sip.delete(oldLayout)
        self.__mainLayout = QtGui.QVBoxLayout()
        self.setLayout(self.__mainLayout)
        
    def showAuthFailedDialog(self, text):
        """Displays a QMessageBox to inform the user about a failed authentication."""
        QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, text, UIText.BUT_OK)
    
    def showConnectionLostDialog(self):
        """Displays a QMessageBox to inform the user about the lost of connection to the internet."""
        QtGui.QMessageBox.information(self, UIText.LABEL_FINISHED, UIText.MSG_CONNECTION_LOST, UIText.BUT_OK)
        
    def closeEvent(self, event):
        """This method is called when the widget is closing. Sends a leave request before closing the widget."""
        LeaveRequest.requestFinish(True)
        event.accept()
        
    #decide next widget
    def nextWidget(self, username, password, isLogin, isNewNetwork):
        """This method decides, which widget to show next after closing the __loginWidget.
        
        If the network database is not set properly it shows the __loginMsnpDBWidget,
        if the media database is not set properly it shows the __loginMediaDBWidget,
        otherwise it displays the __loginSplashWidget.
        """
        msnpDBSettings = XMLController.readAllSettings().getCurrentUserSettings().getMSNPDatabaseSettings()
        if msnpDBSettings.isComplete():
            dbAccessible = PersistenceController.testDBCredentials(msnpDBSettings.getHost(), msnpDBSettings.getUser(), msnpDBSettings.getPassword())
            if dbAccessible:
                mediaDBSettings = XMLController.readAllSettings().getCurrentUserSettings().getMediaDatabaseSettings()
                if mediaDBSettings.isComplete():
                    dbAccessible = PersistenceController.testDBCredentials(msnpDBSettings.getHost(), msnpDBSettings.getUser(), msnpDBSettings.getPassword())
                    if dbAccessible:
                        gui.GUIController.getStartWidget().buildLoginSplashWidget()
                        Initialization.initialize(username, password, isLogin, isNewNetwork)
                    else:
                        QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_DB_ERROR_MEDIADB_NOT_ACCESSIBLE, UIText.BUT_OK)
                        gui.GUIController.getStartWidget().buildLoginMediaDBSettingsWidget(self.__settings, username, password, isLogin, isNewNetwork)
                else:
                    gui.GUIController.getStartWidget().buildLoginMediaDBSettingsWidget(self.__settings, username, password, isLogin, isNewNetwork)
            else:
                QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_DB_ERROR_PTPDB_NOT_ACCESSIBLE, UIText.BUT_OK)
                gui.GUIController.getStartWidget().buildLoginMsnpDBSettingsWidget(self.__settings, username, password, isLogin, isNewNetwork)
        else:
            gui.GUIController.getStartWidget().buildLoginMsnpDBSettingsWidget(self.__settings, username, password, isLogin, isNewNetwork)

class LoginWidget(QtGui.QWidget):
    """This class contains the login widget where the user enters user name and password and can register or login, or create a new network.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __settings: A Settings object containing the stores settings.
        __startWidget: A StartWidget object containing the start widget from which this widget was called.
        __mainLayout: A QVBoxLayout object containing all object of this widget.
        __lblIcon: A QLabel object.
        __textWidget: A QWidget object containing the objects __lblUsername, __txtUsername, __lblPassword and __txtPassword.
        __lblUsername: A QLabel object.
        __txtUsername: A QLineEdit object.
        __lblPassword: A QLabel object.
        __txtPassword: A QLineEdit object.
        __buttonWidget: A QWidget object containing the buttons __butNewNetwork, __butRegister, __butLogin.
        __butNewNetwork: A QPushButton object connected to the method __newNetwork().
        __butRegister: A QPushButton object connected to the method __register().
        __butLogin: A QPushButton object connected to the method __login().
    """
    
    def __init__(self, settings, startWidget, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__mainLayout = QtGui.QVBoxLayout()
        self.__settings = settings
        self.__startWidget = startWidget
        #build
        self.__build()
        self.setLayout(self.__mainLayout)
        
    def __build(self): 
        """Initializes the items contained in the widget."""
        self.__initTextWidget()
        self.__initButtonWidget()
        self.__mainLayout.addWidget(self.__lblIcon)
        self.__mainLayout.addWidget(self.__textWidget)
        self.__mainLayout.addWidget(self.__buttonWidget)
            
    def __initTextWidget(self):
        """Initializes the __textWidget."""
        grid = QtGui.QGridLayout()
        self.__initLabels()
        self.__initLineEdits()
        grid.addWidget(self.__lblUsername, 1, 0)
        grid.addWidget(self.__lblPassword, 2, 0)
        grid.addWidget(self.__txtUsername, 1, 1)
        grid.addWidget(self.__txtPassword, 2, 1)
        self.__textWidget = QtGui.QWidget()
        self.__textWidget.setLayout(grid)
        
    def __initButtonWidget(self):
        """Initializes the __buttonWidget."""
        buttonLayout = QtGui.QHBoxLayout()
        self.__initButtons()
        buttonLayout.addWidget(self.__butNewNetwork)
        buttonLayout.addWidget(self.__butRegister)
        buttonLayout.addWidget(self.__butLogin)
        self.__buttonWidget = QtGui.QWidget()
        self.__buttonWidget.setLayout(buttonLayout)
        
    def __initLabels(self):
        """Initializes the QLabel objects __lblIcon, __lblUsername and __lblPassword."""
        self.__lblIcon = QtGui.QLabel()
        self.__lblIcon.setPixmap(QtGui.QPixmap(Resources.IMAGE_LOGIN_ICON))
        self.__lblIcon.setAlignment(QtCore.Qt.AlignCenter)
        self.__lblUsername = QtGui.QLabel(UIText.LABEL_USERNAME + ":")
        self.__lblPassword = QtGui.QLabel(UIText.LABEL_PASSWORD + ":")
        
    def __initLineEdits(self):
        """Initializes the QLineEdit objects __txtUsername and __txtPassword."""
        self.__txtUsername = QtGui.QLineEdit()
        if self.__settings != None and self.__settings.getCurrentUserSettings().getUsername() != None:
            self.__txtUsername.setText(self.__settings.getCurrentUserSettings().getUsername())
        self.__txtPassword = QtGui.QLineEdit()
        self.__txtPassword.setEchoMode (QtGui.QLineEdit.Password)

        
    def __initButtons(self):
        """Initializes the QPushButton objects __butLogin, __butNewNetwork and __butRegister."""
        #login
        self.__butLogin = QtGui.QPushButton(UIText.BUT_LOGIN, self)
        self.__butLogin.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butLogin.setShortcut(UIText.SC_LOGIN)
        self.__butLogin.setIcon(QtGui.QIcon(Resources.IMAGE_BUT_LOGIN))
        # new network
        self.__butNewNetwork = QtGui.QPushButton(UIText.BUT_NEW_NETWORK, self)
        self.__butNewNetwork.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butNewNetwork.setShortcut(UIText.SC_NEW_NETWORK)
        self.__butNewNetwork.setIcon(QtGui.QIcon(Resources.IMAGE_BUT_NEW_NETWORK))
        #register
        self.__butRegister = QtGui.QPushButton(UIText.BUT_REGISTER, self)
        self.__butRegister.setFocusPolicy(QtCore.Qt.NoFocus)
        self.__butRegister.setShortcut(UIText.SC_REGISTER)
        self.__butRegister.setIcon(QtGui.QIcon(Resources.IMAGE_BUT_REGISTER))
        #register handler
        self.connect(self.__butLogin, QtCore.SIGNAL('clicked()'), self.__login)
        self.connect(self.__butRegister, QtCore.SIGNAL('clicked()'), self.__register)
        self.connect(self.__butNewNetwork, QtCore.SIGNAL('clicked()'), self.__newNetwork)
        
    def __login(self):
        """This method is connected to the __butLogin."""
        if self.__validateInput():
            self.__continue(True, False)
        
    def __register(self):
        """This method is connected to the QPushButton object __butRegister."""
        if self.__validateInput():
            self.__continue(False, False)
        
    def __newNetwork(self):
        """This method is connected to the QPushButton object __butNewNetwork."""
        if self.__validateInput():
            self.__continue(False, True)
            
    def __continue(self, isLogin, isNewNetwork):
        """This method is called by the methods __login(), __register() and __newNetwork(). Displays the next widget."""
        username = unicode(str(self.__txtUsername.text()))
        password = unicode(str(self.__txtPassword.text()))
        #we have to initialize the settings even if no db-credentials are stored
        Initialization.initSettings(username)
        self.__startWidget.nextWidget(username, password, isLogin, isNewNetwork)
        
    def __validateInput(self):
        """This method is called by the methods __login(), __register() and __newNetwork(). Validates the text in __txtUsername and __txtPassword."""
        username = unicode(str(self.__txtUsername.text()))
        password = unicode(str(self.__txtPassword.text()))
        print 'password: ' + password
        if username != None and len(username) > 0 and password != None and len(password) > 0:
            withoutWhitespaces = username.expandtabs().replace(" ", "")
            if len(username) < 200 and len(password) < 200:
                if len(withoutWhitespaces) > 0:
                    return True
                else:
                    QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_WHITESPACE_USERNAME, UIText.BUT_OK)
            else:
                QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_TOO_LONG_CREDENTIALS, UIText.BUT_OK)
        else:    
            QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_ENTER_CREDENTIALS, UIText.BUT_OK)
        return False
    
class LoginMsnpDBSettingsWidget(QtGui.QWidget):
    """This class contains the network database settings widget.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __username: A String containing the user name.
        __password: A String containing the password.
        __isLogin: A boolean value, True if this is a login attempt, False if its a register attempt.
        __isNewNetwork: A boolean value, True if a new network should be created, False if it should connect to an already existing one.
        __settings: A Settings object containing the stores settings.
        __startWidget: A StartWidget object containing the start widget from which this widget was called.
        __mainLayout: A QVBoxLayout object containing the __msnpDBWidget.
        __msnpDBWidget: A QWidget object containing all objects of this widget.
        __lblDBHost: A QLabel object.
        __lblDBSchema: A QLabel object.
        __lblDBUser: A QLabel object.
        __lblDBPassword: A QLabel object.
        __lblTitle: A QLabel object.
        __txtDBHost: A QLineEdit object.
        __txtDBSchema: A QLineEdit object.
        __txtDBUser: A QLineEdit object.
        __txtDBPassword: A QLineEdit object.
        __butSave: A QPushButton object connected to the method __save().
    """
    
    def __init__(self, username, password, isLogin, isNewNetwork, startWidget, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__username = username
        self.__password = password
        self.__isLogin = isLogin
        self.__isNewNetwork = isNewNetwork
        self.__startWidget = startWidget
        self.__settings = XMLController.readAllSettings()
        self.__mainLayout = QtGui.QVBoxLayout()
        #build
        self.__build()
        self.setLayout(self.__mainLayout)
        
    def __build(self):
        """Initializes the items contained in the widget."""
        self.__initLabels()
        self.__initLineEdits()
        self.__initButton()
        self.__initDBWidget()
        self.__mainLayout.addWidget(self.__msnpDBWidget)
    
    def __initDBWidget(self):
        """Adds all items to the widget."""
        #create gridLayout
        gridLayout = QtGui.QGridLayout()
        gridLayout.addWidget(self.__lblDBHost, 1, 0)
        gridLayout.addWidget(self.__lblDBSchema, 2, 0)
        gridLayout.addWidget(self.__lblDBUser, 3, 0)
        gridLayout.addWidget(self.__lblDBPassword, 4, 0)
        gridLayout.addWidget(self.__txtDBHost, 1, 1)
        gridLayout.addWidget(self.__txtDBSchema, 2, 1)
        gridLayout.addWidget(self.__txtDBUser, 3, 1)
        gridLayout.addWidget(self.__txtDBPassword, 4, 1)
        #widget for gridlayout
        gridWidget = QtGui.QWidget()
        gridWidget.setLayout(gridLayout)
        #add widgets to vlayout
        vLayout = QtGui.QVBoxLayout()
        vLayout.addWidget(self.__lblTitle)
        vLayout.addWidget(gridWidget)
        vLayout.addWidget(self.__butSave)
        #add vlayout to playerDBWidget
        self.__msnpDBWidget = QtGui.QWidget()
        self.__msnpDBWidget.setLayout(vLayout)
    
    def __initLabels(self):
        """Initializes the QLabel objects __lblTitle, __lblDBHost, __lblDBUser, __lblDBSchema and __lblDBPassword.""" 
        self.__lblTitle = QtGui.QLabel(UIText.LABEL_MSNP_DB_SETTINS)
        self.__lblTitle.setFont(MyStyle.getFont18Bold())
        self.__lblTitle.setAlignment(QtCore.Qt.AlignCenter)
        self.__lblTitle.setFixedHeight(50)
        self.__lblDBHost = QtGui.QLabel(UIText.LABEL_HOST + ":")
        self.__lblDBUser = QtGui.QLabel(UIText.LABEL_USER + ":")
        self.__lblDBSchema = QtGui.QLabel(UIText.LABEL_SCHEMA + ":")
        self.__lblDBPassword = QtGui.QLabel(UIText.LABEL_PASSWORD + ":")
        
    def __initButton(self):
        """Initializes the QPushButton object __butSave."""
        self.__butSave = QtGui.QPushButton(UIText.BUT_SAVE, self)
        self.__butSave.setFocusPolicy(QtCore.Qt.NoFocus)
        #register handler
        self.connect(self.__butSave, QtCore.SIGNAL('clicked()'), self.__save)
        
    def __initLineEdits(self):
        """Initializes the QLineEdit objects __txtDBHost, __txtDBUser, __txtDBSchema and __txtDBPassword."""
        self.__txtDBHost = QtGui.QLineEdit()
        self.__txtDBUser = QtGui.QLineEdit()
        self.__txtDBSchema = QtGui.QLineEdit()
        self.__txtDBPassword = QtGui.QLineEdit()
        self.__txtDBPassword.setEchoMode (QtGui.QLineEdit.Password)
        #TODO only during testing
        self.__txtDBHost.setText("localhost")
        self.__txtDBUser.setText("root")
        self.__txtDBSchema.setText("msnp_" + self.__username)
        self.__txtDBPassword.setText("root")
        
    def __getDBSettings(self):
        """Retrieves the text from the QLineEdit objects __txtDBHost, __txtDBUser, __txtDBSchema and __txtDBPassword and returns it as a DatabaseSettings object."""
        host = unicode(str(self.__txtDBHost.text()))
        user = unicode(str(self.__txtDBUser.text()))
        password = unicode(str(self.__txtDBPassword.text()))
        schema = unicode(str(self.__txtDBSchema.text()))
        return SettingsModel.DatabaseSettings(host, user, password, schema, "")
    
    def __validateInput(self):
        """Checks, whether the QLineEdit objects __txtDBHost, __txtDBUser, __txtDBSchema and __txtDBPassword are not empty."""
        if self.__txtDBHost.text() != None and len(self.__txtDBHost.text()) > 0 and self.__txtDBUser.text() != None and len(self.__txtDBUser.text()) > 0 and self.__txtDBSchema.text() != None and len(self.__txtDBSchema.text()) > 0:
            return True
        return False
    
    def __save(self):
        """This method is called by the QPushButton object __butSave."""
        inputOk = self.__validateInput()
        if inputOk:
            msnpSettings = self.__getDBSettings()
            self.__settings.getCurrentUserSettings().setMSNPDatabaseSettings(msnpSettings)
            XMLController.storeSettings(self.__settings)
            self.__startWidget.nextWidget(self.__username, self.__password, self.__isLogin, self.__isNewNetwork)
        else:
            QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_COMPLETE_DB_SETTINGS, UIText.BUT_OK)


class LoginMediaDBSettingsWidget(QtGui.QWidget):
    """This class contains the media database settings widget.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __username: A String containing the user name.
        __password: A String containing the password.
        __isLogin: A boolean value, True if this is a login attempt, False if its a register attempt.
        __isNewNetwork: A boolean value, True if a new network should be created, False if it should connect to an already existing one.
        __settings: A Settings object containing the stores settings.
        __startWidget: A StartWidget object containing the start widget from which this widget was called.
        __mainLayout: A QVBoxLayout object containing the __mediaDBWidget.
        __mediaDBWidget: A QWidget object containing all objects of this widget.
        __lblDBHost: A QLabel object.
        __lblDBSchema: A QLabel object.
        __lblDBUser: A QLabel object.
        __lblDBPassword: A QLabel object.
        __lblTitle: A QLabel object.
        __txtDBHost: A QLineEdit object.
        __txtDBSchema: A QLineEdit object.
        __txtDBUser: A QLineEdit object.
        __txtDBPassword: A QLineEdit object.
        __butSave: A QPushButton object connected to the method __save().
    """
    
    def __init__(self, username, password, isLogin, isNewNetwork, startWidget, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__username = username
        self.__password = password
        self.__isLogin = isLogin
        self.__isNewNetwork = isNewNetwork
        self.__startWidget = startWidget
        self.__settings = XMLController.readAllSettings()
        self.__mainLayout = QtGui.QVBoxLayout()
        #build
        self.__build()
        self.setLayout(self.__mainLayout)
        
    def __build(self): 
        """Initializes the items contained in the widget."""
        self.__initLabels()
        self.__initLineEdits()
        self.__initButton()
        self.__initDBWidget()
        self.__mainLayout.addWidget(self.__mediaDBWidget)
    
    def __initDBWidget(self):
        """Adds all items to the widget."""
        #create gridLayout
        gridLayout = QtGui.QGridLayout()
        gridLayout.addWidget(self.__lblDBHost, 1, 0)
        gridLayout.addWidget(self.__lblDBSchema, 2, 0)
        gridLayout.addWidget(self.__lblDBUser, 3, 0)
        gridLayout.addWidget(self.__lblDBPassword, 4, 0)
        gridLayout.addWidget(self.__txtDBHost, 1, 1)
        gridLayout.addWidget(self.__txtDBSchema, 2, 1)
        gridLayout.addWidget(self.__txtDBUser, 3, 1)
        gridLayout.addWidget(self.__txtDBPassword, 4, 1)
        #widget for gridlayout
        gridWidget = QtGui.QWidget()
        gridWidget.setLayout(gridLayout)
        #add widgets to vlayout
        vLayout = QtGui.QVBoxLayout()
        vLayout.addWidget(self.__lblTitle)
        vLayout.addWidget(gridWidget)
        vLayout.addWidget(self.__butSave)
        #add vlayout to playerDBWidget
        self.__mediaDBWidget = QtGui.QWidget()
        self.__mediaDBWidget.setLayout(vLayout)
    
    def __initLabels(self):
        """Initializes the QLabel objects __lblTitle, __lblDBHost, __lblDBUser, __lblDBSchema and __lblDBPassword.""" 
        self.__lblTitle = QtGui.QLabel(UIText.LABEL_MEDIA_DB_SETTINGS)
        self.__lblTitle.setFont(MyStyle.getFont18Bold())
        self.__lblTitle.setAlignment(QtCore.Qt.AlignCenter)
        self.__lblTitle.setFixedHeight(50)
        self.__lblDBHost = QtGui.QLabel(UIText.LABEL_HOST + ":")
        self.__lblDBUser = QtGui.QLabel(UIText.LABEL_USER + ":")
        self.__lblDBSchema = QtGui.QLabel(UIText.LABEL_SCHEMA + ":")
        self.__lblDBPassword = QtGui.QLabel(UIText.LABEL_PASSWORD + ":")
        
    def __initButton(self):
        """Initializes the QPushButton object __butSave."""
        self.__butSave = QtGui.QPushButton(UIText.BUT_SAVE, self)
        self.__butSave.setFocusPolicy(QtCore.Qt.NoFocus)
        #register handler
        self.connect(self.__butSave, QtCore.SIGNAL('clicked()'), self.__save)
        
    def __initLineEdits(self):
        """Initializes the QLineEdit objects __txtDBHost, __txtDBUser, __txtDBSchema and __txtDBPassword."""
        self.__txtDBHost = QtGui.QLineEdit()
        self.__txtDBUser = QtGui.QLineEdit()
        self.__txtDBSchema = QtGui.QLineEdit()
        self.__txtDBPassword = QtGui.QLineEdit()
        self.__txtDBPassword.setEchoMode (QtGui.QLineEdit.Password)
        #TODO only during testing
        self.__txtDBHost.setText("localhost")
        self.__txtDBUser.setText("root")
        self.__txtDBSchema.setText("media_db")
        self.__txtDBPassword.setText("root")
        
    def __getDBSettings(self):
        """Retrieves the text from the QLineEdit objects __txtDBHost, __txtDBUser, __txtDBSchema and __txtDBPassword and returns it as a DatabaseSettings object."""
        host = unicode(str(self.__txtDBHost.text()))
        user = unicode(str(self.__txtDBUser.text()))
        password = unicode(str(self.__txtDBPassword.text()))
        schema = unicode(str(self.__txtDBSchema.text()))
        return SettingsModel.DatabaseSettings(host, user, password, schema, "")
    
    def __validateInput(self):
        """Checks, whether the QLineEdit objects __txtDBHost, __txtDBUser, __txtDBSchema and __txtDBPassword are not empty."""
        if self.__txtDBHost.text() != None and len(self.__txtDBHost.text()) > 0 and self.__txtDBUser.text() != None and len(self.__txtDBUser.text()) > 0 and self.__txtDBSchema.text() != None and len(self.__txtDBSchema.text()) > 0:
            return True
        return False
    
    def __save(self):
        """This method is called by the QPushButton object __butSave."""
        inputOk = self.__validateInput()
        if inputOk:
            mediaSettings = self.__getDBSettings()
            self.__settings.getCurrentUserSettings().setMediaDatabaseSettings(mediaSettings)
            XMLController.storeSettings(self.__settings)
            self.__startWidget.nextWidget(self.__username, self.__password, self.__isLogin, self.__isNewNetwork)
        else:
            QtGui.QMessageBox.information(self, UIText.MSG_ATTENTION, UIText.MSG_COMPLETE_DB_SETTINGS, UIText.BUT_OK)
        
class LoginSplashWidget(QtGui.QWidget):
    """This class contains the login splash widget which is displayed while waiting for the authentication.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __mainLayout: A QVBoxLayout object containing all items of the widget.
        __lblSplash: A QLabel object.
        __lblLoading: A QLabel object.
        __movie: A QMovie object.
    """
    
    def __init__(self, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__mainLayout = QtGui.QVBoxLayout()
        #build
        self.__build()
        self.setLayout(self.__mainLayout)
        
    def __build(self): 
        """Adds the items to the widget."""
        self.__initSplashLabels()
        self.__mainLayout.addWidget(self.__lblSplash)
        self.__mainLayout.addWidget(self.__lblLoading)
        
    def __initSplashLabels(self):
        """Initializes the items contained in the widget."""
        #movie container
        self.__lblSplash = QtGui.QLabel()
#        self.__lblSplash.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.__lblSplash.setAlignment(QtCore.Qt.AlignCenter)
        #create movie
        self.__movie = QtGui.QMovie(Resources.IMAGE_LOADING, QtCore.QByteArray(), self)
        self.__movie.setCacheMode(QtGui.QMovie.CacheAll)
        self.__movie.setSpeed(100)
        self.__lblSplash.setMovie(self.__movie)
        self.__movie.start()
        self.__lblLoading = QtGui.QLabel(UIText.LABEL_LOADING)
        self.__lblLoading.setAlignment(QtCore.Qt.AlignHCenter)
        
